package com.future.hair.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.entity.Category;
import com.future.hair.server.entity.User;
import com.future.hair.server.entity.UserInfo;
import com.future.hair.server.entity.Works;
import com.future.hair.server.mapper.CategoryMapper;
import com.future.hair.server.mapper.UserInfoMapper;
import com.future.hair.server.mapper.UserMapper;
import com.future.hair.server.mapper.WorksMapper;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * @author future-RogueYBJ
 * @version v1.0
 * @since 2020-05-23
 */

@RestController
@RequestMapping("//hair/works")
@Api(tags = "Works管理接口")

public class WorksController {
    @Autowired
    private WorksMapper mapper;

    @Autowired
    private UserInfoMapper infoMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "查询 Works 列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<List<Works>> worksList(@RequestBody PageModel data) {
        QueryWrapper<Works> worksWrapper = new QueryWrapper<Works>();
        worksWrapper.eq("category_id",data.getCategoryId());
        return CommonResult.data(mapper.selectPage(new Page<>(data.getSize(), data.getCurrent()), null).getRecords());
    }

    @ApiOperation(value = "查询当前 Works")
    @RequestMapping(value = "/id", method = RequestMethod.POST)
    public CommonResult<Works> worksId(@RequestBody Works data) {
        return CommonResult.data(mapper.selectById(data.getId()));
    }

    @ApiOperation(value = "增加 Works")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Integer> create(@RequestBody Works data, @RequestHeader String token) {
        if (null == data.getContent() || null == data.getImages() || null == data.getCategoryId()) {
            return CommonResult.failed("参数不能为空");
        }

        List<String> imageList = Arrays.asList(data.getImages().split(";;"));

        if (imageList.size()>=1){
            data.setCover(imageList.get(0));
        }

        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<Category>();
        categoryQueryWrapper.eq("id", data.getCategoryId()); //请输入排异条件
        categoryQueryWrapper.eq("type", 2); //请输入排异条件
        if (null == categoryMapper.selectOne(categoryQueryWrapper)) {
            return CommonResult.failed("没有当前类型");
        }

        Integer userId = JwtHelper.getByUserId(token);
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
        wrapper.eq("user_id", userId); //请输入排异条件
        UserInfo info = infoMapper.selectOne(wrapper);
        if (null == info) {
            return CommonResult.failed("用户不是设计师");
        }



        data.setUserInfoId(info.getId());
        data.setCreateTime(null);
        data.setUpdataTime(null);
        Integer result = mapper.insert(data);
        if (result == 1) {
            return CommonResult.data(result);
        } else {
            return CommonResult.failed("创建失败");
        }
    }

    @ApiOperation(value = "更新 Works")
    @RequestMapping(value = "/upData", method = RequestMethod.POST)
    public CommonResult<Integer> upData(@RequestBody Works data, @RequestHeader String token) {
        return CommonResult.failed("用户不是设计师");
//        if (null == JwtHelper.getByOpenId(token) || JwtHelper.getByOpenId(token) < 4444) {
//            QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
//            wrapper.eq("user_id", JwtHelper.getByUserId(token)); //请输入排异条件
//            UserInfo info = infoMapper.selectOne(wrapper);
//            if (null == info) {
//                return CommonResult.failed("用户不是设计师");
//            }
//        }
//        data.setUserId(null);
//        data.setMallId(null);
//        data.setUserInfoId(null);
//        data.setCreateTime(null);
//        data.setUpdataTime(null);
//        data.setCategoryId(null);
//        Integer result = mapper.updateById(data);
//        if (result == 1) {
//            return CommonResult.data(result);
//        } else {
//            return CommonResult.failed("更新失败");
//        }
    }

    @ApiOperation(value = "删除 Works")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult<Integer> delete(@RequestBody Works data, @RequestHeader String token) {
        if (null == JwtHelper.getByOpenId(token) && JwtHelper.getByOpenId(token) >= 4444) {
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
            wrapper.eq("user_id", JwtHelper.getByUserId(token)); //请输入排异条件
            UserInfo info = infoMapper.selectOne(wrapper);
            if (null == info) {
                return CommonResult.failed("用户不是设计师");
            }
        }

        Integer result = mapper.deleteById(data.getId());
        if (result == 1) {
            return CommonResult.data(result);
        } else {
            return CommonResult.failed("删除失败");
        }
    }
}